package com.github.dice.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.github.dice.entity.Room;
import com.github.dice.entity.RoomOwner;
import com.github.dice.service.IRoomService;

@Controller
@RequestMapping("/page")
public class PageController extends BaseController {

    @Resource
    private IRoomService roomService;

    @RequestMapping("/player/login")
    public ModelAndView playerLogin(ModelAndView mv) {
        mv.setViewName("/player/playerLogin.html");
        return mv;
    }

    @RequestMapping("/roomOwner/login")
    public ModelAndView roomOwnerLogin(ModelAndView mv, HttpServletRequest request) {
        mv.setViewName("/roomOwner/roomOwnerLogin.html");
        return mv;
    }

    @RequestMapping("/player/rollDice")
    public ModelAndView rollDice(@RequestParam("roomNumber") String roomNumber, ModelAndView mv,
                                 HttpServletRequest request) {
        if (checkPlayerIsLogin(request)) {
            Room room = roomService.getRoomByNumber(roomNumber);
            mv.addObject("room", room);
            mv.setViewName("/player/rollDice.html");
        } else {
            mv.setViewName("/player/playerLogin.html");
        }
        return mv;
    }

    @RequestMapping("/player/joinHome")
    public ModelAndView joinHome(ModelAndView mv, HttpServletRequest request) {
        if (checkPlayerIsLogin(request)) {
            mv.setViewName("/player/joinHome.html");
        } else {
            mv.setViewName("/player/playerLogin.html");
        }
        return mv;
    }

    @RequestMapping("/roomOwner/openRoom")
    public ModelAndView openRoom(ModelAndView mv, HttpServletRequest request) {
        if (checkRoomOwnerIsLogin(request)) {
            mv.setViewName("/roomOwner/openRoom.html");
        } else {
            mv.setViewName("/roomOwner/roomOwnerLogin.html");
        }
        return mv;
    }

    @RequestMapping("/roomOwner/roomList")
    public ModelAndView roomList(ModelAndView mv, HttpServletRequest request) {
        List<Room> list = new ArrayList<>();
        Object roomOwnerSession = request.getSession().getAttribute("room_owner_login_data");
        if (null != roomOwnerSession) {
            String roomOwnerName = ((RoomOwner) roomOwnerSession).getRoomOwnerName();
            list = roomService.getRoomListByRoomOwnerName(roomOwnerName);
            mv.addObject("rooms", list);
            mv.setViewName("/roomOwner/roomList.html");
        } else {
            mv.setViewName("/roomOwner/roomOwnerLogin.html");
        }
        return mv;
    }

    @RequestMapping("/roomOwner/roomDetail")
    public ModelAndView roomDetail(@RequestParam("roomNumber") String roomNumber, ModelAndView mv,
                                   HttpServletRequest request) {
        if (checkRoomOwnerIsLogin(request)) {
            Room room = roomService.getRoomByNumber(roomNumber);
            mv.addObject("room", room);
            mv.setViewName("/roomOwner/roomDetail.html");
        } else {
            mv.setViewName("/roomOwner/roomOwnerLogin.html");
        }
        return mv;
    }

}
